package com.defysoft.waukidsschool;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Sqlite_Connect {

	Cursor cursor;
	Sqlite_OpenHelper so;
	public SQLiteDatabase db;
ArrayList<String> list;
	public Sqlite_Connect(Context context) {

		so = new Sqlite_OpenHelper(context, "waukidsschool.db", null, 1);
		this.db = so.getWritableDatabase();
	}

	public void close_Connect() {
		so.close();
		db.close();
	}

	/**
	 * 원아수첩 조회수채크 여부검사 매개인자로 글의 pk값 과 지금 로그인된 학생의 코드값을 넘기면 db에 조회된 값이 있으면 true
	 * 없으면 false반환
	 * */
	public Cursor check_School() {
		list=new ArrayList<String>();
		cursor = db.rawQuery("select * from SCHOOL ORDER BY PK_School_Code DESC ", null);
		/*while (cursor.moveToNext()) {
			list.add("유형 : "+(cursor.getInt(2)==1? "에러로그 ":"일반로그 ")+"에러내용 : "+cursor.getString(1)+" 시간 : "+cursor.getString(0));
			
		}*/
		return cursor;
	}
	
	public Cursor check_School(int stucode) {
		list=new ArrayList<String>();
		cursor = db.rawQuery("select * from SCHOOL WHERE School_Type = 1 AND School_Type = 3  ORDER BY PK_School_Code DESC ", null);

		return cursor;
	}
	
	

	public boolean insert_School(String code, int type) {
		SimpleDateFormat sdfNow = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdfNow.format(new Date(System.currentTimeMillis()));
		db.execSQL("insert into SCHOOL values(DATETIME('"+time+"'),'" + code + "'," + type + ")");

		return true;
	}
	public Cursor save_School() {
		list=new ArrayList<String>();
		cursor = db.rawQuery("select * from SCHOOL WHERE School_Type =2 OR School_Type =3  ORDER BY PK_School_Code DESC LIMIT 300", null);
		/*while (cursor.moveToNext()) {
			list.add("유형 : "+(cursor.getInt(2)==1? "에러로그 ":"일반로그 ")+"에러내용 : "+cursor.getString(1)+" 시간 : "+cursor.getString(0));
			
		}*/
		return cursor;
	}
	
	public void update_School(){
		db.execSQL("UPDATE SCHOOL SET School_Type=School_Type-2 WHERE (School_Type =2 OR School_Type =3)");
	}


}
